package com.ztgf.logistics.api.client.deppon;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.constant.SysConstant;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.vo.UserVO;
import com.ztgf.logistics.models.dto.deppon.LogisticsBillDTO;
import com.ztgf.logistics.models.dto.deppon.LogisticsUserContractDTO;
import com.ztgf.logistics.models.service.deppon.ILogisticsBillService;
import com.ztgf.logistics.models.service.deppon.ILogisticsUserContractService;
import com.ztgf.logistics.models.service.deppon.ILogisticsUserInfoService;
import com.ztgf.logistics.models.vo.deppon.LogisticsUserAuditVO;
import com.ztgf.logistics.models.vo.deppon.LogisticsUserVO;
import com.ztgf.shopping.models.dto.audit.AuditDTO;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.service.audit.IAuditService;
import com.ztgf.shopping.models.service.shop.IShopContractSettleInService;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.sys.models.service.ISessionService;
import com.ztgf.sys.models.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ztgf.logistics.models.service.deppon.ILogisticsUserService;
import com.ztgf.logistics.models.bo.deppon.LogisticsUserBO;
import com.ztgf.logistics.models.dto.deppon.LogisticsUserDTO;

import java.math.BigDecimal;

/**
 * 物流用户信息表控制器
 *
 * @author mxli
 * @Date 2021-09-13 22:16:41
 */

@RestController
@RequestMapping("/api/logisticsUser")
@Slf4j
@Api(value = "LogisticsUserApiController",tags = "物流用户信息表Api接口")
public class LogisticsUserApiController {
    @DubboReference
    private IShopContractSettleInService shopContractSettleInService;
    @DubboReference
    private ILogisticsUserService logisticsUserService;
    @DubboReference
    private IShopInfoService shopInfoService;
    @DubboReference
    private ISysConfigService sysConfigService;
    @DubboReference
    private ISessionService sessionService;
    @DubboReference
    private IAuditService auditService;
    @DubboReference
    private ILogisticsUserContractService logisticsUserContractService;
    @DubboReference
    private ILogisticsBillService logisticsBillService;
    @DubboReference
    private ILogisticsUserInfoService logisticsUserInfoService;



    @PostMapping("/get")
    @ApiOperation(value = "根据店铺id查找",notes = "根据店铺id查找")
    @ApiImplicitParam(name = "shopId",value = "店铺id",required = true,paramType = "query", dataType = "Long")
    public Result<LogisticsUserAuditVO> get(@RequestParam("shopId") Long shopId){
        return logisticsUserInfoService.get(shopId);
    }

    @PostMapping("/signContract")
    @ApiOperation(value = "签合同",notes = "签合同")
    @ApiImplicitParam(name = "shopId",value = "店铺id",required = true,paramType = "query", dataType = "Long")
    public Result<String> signContract(Long shopId){
        LogisticsUserDTO dto = logisticsUserService.selectById(shopId);
        if(dto == null || dto.getType() == 1 || dto.getType() == 2){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        if(dto.getContractStatus() == 0 || dto.getContractStatus()==2){
            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("shop_id", shopId);
            wrapper.eq("status", 0);
            wrapper.orderBy("id", false);
            wrapper.last("limit 1");
            LogisticsUserContractDTO contractDTO = logisticsUserContractService.selectOne(wrapper);
            if(contractDTO == null){
                //创建合同
                UserVO userVO = sessionService.getUserVO();
                contractDTO = new LogisticsUserContractDTO();
                contractDTO.setCreateUserId(userVO.getId());
                contractDTO.setShopId(shopId);
                contractDTO.setTempletId(null);
                contractDTO.setTempletName(null);
                contractDTO.setContractId(null);
                contractDTO.setFileUrl(null);
                 return  logisticsUserContractService.add(contractDTO);

            }else{
                return  logisticsUserContractService.add(contractDTO);
            }
        }
        return new Result();
    }

    @PostMapping("/getCallBackDeBangContactInfo")
    @ApiOperation(value = "德邦合同回调",notes = "德邦合同回调")
    public String getCallBackDeBangContactInfo(@RequestParam(required = false) String content,@RequestParam(required = false) String signature,@RequestParam(required = false) String timestamp) throws Exception {
        return shopContractSettleInService.getCallBackDeBangContactInfo(signature, timestamp, content);
    }
}
